package com.reebake.ideal.message;

import com.reebake.ideal.jdbc.FlywayUtil;
import com.reebake.ideal.message.config.MessageConfig;
import com.reebake.ideal.message.properties.MessageProperties;
import org.flywaydb.core.Flyway;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import javax.sql.DataSource;

@Configuration
@EnableConfigurationProperties(MessageProperties.class)
@ComponentScan(basePackageClasses = {MessageAutoConfiguration.class})
@Import(MessageConfig.class)
public class MessageAutoConfiguration {

    @Bean
    MapperScannerConfigurer messageMapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage(MessageAutoConfiguration.class.getPackageName() + ".mapper");
        return mapperScannerConfigurer;
    }

    @Bean
    Flyway messageFlyway(DataSource dataSource) {
        String schema = "message";
        return FlywayUtil.migrate(dataSource, schema);
    }
}
